/**
 * Copyright (c) 2012-2013,Epic-HUST Technology(Wuhan)Co.,Ltd. All Rights Reserved.
 */
package com.mnt.gui.base.consult;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.ResourceBundle;

import javafx.fxml.FXMLLoader;


/**
 * FXML工具类
 * <p>
 * 针对FXML的帮助类，提供FXML==>Node.
 * </p>
 * FXML为
 * 
 * @author Albert
 * @version 3.1
 * @since 2013年9月12日 下午1:10:16 Albert .
 */
public class FXMLHelper
{
    public static ClassLoader classLoader = FXMLHelper.class.getClassLoader();

    /**
     * Loads an fxml resource using the simple name of root's class and sets root as the component tree root and controller.
     */
    public static void load(Object root)
    {
        load(root, root);
    }

    /**
     * Loads an fxml resource using the simple name of root's class and sets root as the component tree root and controller as the controller.
     */
    public static void load(Object root, Object controller)
    {
        load(root, controller, Collections.<String, Object> emptyMap());
    }

    /**
     * Loads an fxml resource using the simple name of root's class and sets root as the component tree root and controller as the controller. Uses mapping for controlling namespaces.
     */
    public static void load(Object root, Object controller, Map<String, ? extends Object> mapping)
    {
        String fileName = root.getClass().getSimpleName() + ".fxml";
//        String i18nName = root.getClass().getName();
        
        int dotIndexLast = root.getClass().getName().lastIndexOf(".");
        @SuppressWarnings("unused")
		String i18nName = root.getClass().getName().substring(0,dotIndexLast)+".i18n"+root.getClass().getName().substring(dotIndexLast);

        URL location = root.getClass().getResource(fileName);
        if (null == location)
        {
            throw new RuntimeException("未找到 " + fileName + "。如果 " + root.getClass() + " 继承Class A, then class Zuper must use a version of FXMLUtils.load() "
                    + "that explicitly defines the path to the FXML. " + "E.g. FXMLUtils.load( this, this, Zuper.class.getResource( Zuper.class.getSimpleName() + '.fxml' ) ).");
        }

        // 完善国际化
//        ResourceBundle i18nBUNDLE = ResourceBundle.getBundle(i18nName, UniMaxClient.locale());

        load(root, controller, mapping, location, null);
    }

    /**
     * Loads an fxml resource from resourceName and sets root as the component tree root and controller as the controller.
     */
    public static void load(Object root, Object controller, URL resourceName)
    {
        load(root, controller, Collections.<String, Object> emptyMap(), resourceName);
    }

    /**
     * <p>
     * Loads an fxml resource from resourceName and sets root as the component tree root and controller as the controller. Uses mapping for controlling namespaces.
     * </p>
     * 
     * @since 2013年9月12日 下午1:12:12 Albert
     * @param root
     *            传入的object
     * @param controller
     *            传入的控制器
     * @param mapping
     *            参数值
     * @param resourceName
     *            FXML资源文件Url.
     */
    public static void load(Object root, Object controller, Map<String, ? extends Object> mapping, URL location)
    {
        load(root, controller, mapping, location, null);
    }

    public static void load(Object root, Object controller, Map<String, ? extends Object> mapping, URL location, ResourceBundle i18nBundle)
    {
        FXMLLoader loader = null;

        if (i18nBundle != null)
        {
            loader = new FXMLLoader(location, i18nBundle);
        } else
        {
        	loader = new FXMLLoader(location);
        }
        loader.setClassLoader(classLoader);
        loader.setRoot(root);

        // Sets the controller associated with the root object.
        //TODO ALbert 带测试此问题
        loader.setController(controller);

        if (mapping != null && !mapping.isEmpty())
        {
            loader.getNamespace().putAll(mapping);
        }

        try
        {
            //此时使用的是默认的JavaFXBuilderFactory.
            loader.load();
        } catch (IOException exception)
        {
            throw new RuntimeException("加载资源[" + location + "]出现异常.");
        }
    }
}
